
Promise.MyAll = function (promises) {
  let arr = [],
    count = 0
  return new Promise((resolve, reject) => {
    promises.forEach((item, i) => {
      Promise.resolve(item).then(res => {  // 使用Promise.resolve(item) 保证参数正确性，能够将非promise的参数转化成 promise !!!!
        arr[i] = res  // 使用arr[i] 下标保存，因为Promises 里面的promise是异步的，为了使参数promises和返回值对应需要用arr[i]进行保存
        count += 1
        if (count === promises.length) resolve(arr) // 当所有promise都成功时候，count = promises.length时候菜返回。
      }).catch(reject)
    })
  })
}

Promise.MyAll2 = function (promises) {
  let arr = [],
    count = 0
  return new Promise((resolve, reject) => {
    promises.forEach((item, i) => {
      Promise.resolve(item).then(res => {
        arr[i] = res
        count += 1
        if (count === promises.length) resolve(arr)
      }, reject)
    })
  })
}



// ---------------------------- test

